/*!
Copyright (c) REBUILD <https://getrebuild.com/> and/or its owners. All rights reserved.

rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/


const wpc = window.__PageConfig

const _RbList_renderAfter = RbList.renderAfter
RbList.renderAfter = function () {
  typeof _RbList_renderAfter === 'function' && _RbList_renderAfter()
  parent && parent.referenceSearch__dlg && parent.referenceSearch__dlg.resize()
}


const _RbList_queryBefore = RbList.queryBefore
RbList.queryBefore = function (query) {
  if (typeof _RbList_queryBefore === 'function') {
    query = _RbList_queryBefore(query)
  }

  const formComp412 = parent.referenceSearch__form
  const viewComp = parent.RbViewPage ? parent.RbViewPage._RbViewForm : null
  if (wpc.protocolFilter && parent.referenceSearch__dlg && (formComp412 || viewComp)) {
    let varRecord = formComp412 ? formComp412.getFormData() : viewComp ? viewComp.__ViewData : null
    if (varRecord) {
      
      for (let k in varRecord) {
        if (varRecord[k] && (varRecord[k] + '').length > 100) {
          console.log('Ignore large value of field :', k, varRecord[k])
          delete varRecord[k]
        }
      }
      query.protocolFilter__varRecord = { 'metadata.entity': (formComp412 || viewComp).props.entity, ...varRecord }
    }
  }
  return query
}

$(document).ready(() => {
  
  window.addEventListener('storage', (e) => {
    if (e.key === 'referenceSearch__reload') {
      localStorage.removeItem('referenceSearch__reload')
      RbListPage.reload()
    }
  })

  
  $('.J_select').on('click', () => {
    const ids = RbListPage._RbList.getSelectedIds()
    if (ids.length && parent && parent.referenceSearch__call) parent.referenceSearch__call(ids)
  })

  
  AdvFilters.init('.adv-search', wpc.entity[0])

  
  if (window.__LAB_REFSEARCH_ASIDE42 && wpc.advListAsideShows && wpc.advListAsideShows.length) {
    $('.rb-content').addClass('rb-content-aside')
    
    CategoryWidget.init()
    $('#asideShows a:eq(0)').trigger('click')
  }
})
